package com.hunttown.common.dao.datasource;

import org.springframework.core.NamedThreadLocal;

public abstract class MasterSlaveSwitcher {
    private static final ThreadLocal<Boolean> masterSlaveThreadLocal = new NamedThreadLocal("master slave datasource");

    public MasterSlaveSwitcher() {
    }

    public static final void forceReadMaster() {
        masterSlaveThreadLocal.set(Boolean.FALSE);
    }

    public static final void readSlave() {
        masterSlaveThreadLocal.set(Boolean.TRUE);
    }

    public static final boolean isForceReadMaster() {
        return Boolean.FALSE.equals(masterSlaveThreadLocal.get());
    }

    public static final boolean isReadSlave() {
        return Boolean.TRUE.equals(masterSlaveThreadLocal.get());
    }

    public static final void clearMasterSlave() {
        masterSlaveThreadLocal.remove();
    }
}